package sq.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sq.dao.User;
import sq.service.VCodeValidationService;
import sq.service.ValidationUsedService;
import sq.vo.BaseRespVO;

/**
 * 短信验证码验证校验视图层
 * sunquan
 */
@RestController
public class ValidationController {
    @Autowired
    private VCodeValidationService vCodeValidationServiceImpl;
    @Autowired
    private ValidationUsedService judgeUserNameExistenceService;
    @Autowired
    private ValidationUsedService phoneValidationService;

    /**
     * 验证短信接口实现
     */
    @PostMapping("/validation_vCode")
    public BaseRespVO vCodeValidated(@RequestParam String vCode, @RequestParam String phone) {
        Integer flag = vCodeValidationServiceImpl.vCodeValidated(vCode, phone);
        BaseRespVO baseRespVO = null;
        if (flag < 0) {
            baseRespVO = new BaseRespVO("0", "proving successful");
        } else if (flag == 0) {
            baseRespVO = new BaseRespVO("1", "the phoneNum you print is different from the system send");
        } else if (flag == 1) {
            baseRespVO = new BaseRespVO("2", "the message is out of expireTime");
        } else if (flag == 2) {
            baseRespVO = new BaseRespVO("3", "the message do not exist ");
        }
        return baseRespVO;
    }

    /**
     * 判断用户名是否可用的Controller层
     *
     * @param account 开发者姓名
     * @return baseRespVO 应答结果码实体类
     */
    @PostMapping("/validation_username")
    public BaseRespVO validationPhone(@RequestParam String account) {
        User user = judgeUserNameExistenceService.judgeExist(account);
        return null != user ?
                new BaseRespVO("1", "the user has exist") :
                new BaseRespVO("0", "the user can register");
    }

    /**
     * 判断手机号是否可用
     *
     * @param phone 开发者手机号
     * @return 基础VO层
     */
    @PostMapping("/validation_phone")
    public BaseRespVO judgePhoneExistence(@RequestParam String phone) {
        return phoneValidationService.judgePhoneExistence(phone) ?
                new BaseRespVO("0", "this phoneNum can be in use") :
                new BaseRespVO("1", "the phoneNum has already been in use,please try another number from yours");
    }

    @PostMapping("/validation_password")
    public BaseRespVO judgePassword(@RequestParam String password){
        return phoneValidationService.judgePassword(password) ?
                new BaseRespVO("0", "合法的密码") :
                new BaseRespVO("1", "密码不合法");
    }
}
